//
//  HostRegiViewController.m
//  penzer
//
//  Created by caiyc on 2018/10/4.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "HostRegiViewController.h"
#import "DetectionViewController.h"
@interface HostRegiViewController ()<UITextFieldDelegate,UIScrollViewDelegate>
@property(assign)BOOL checkRule;//是否已勾选服务协议
@property(nonatomic,strong)NSString *base_Str;
@end

@implementation HostRegiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation:1 andTitle:@"注册" andNaviColor:nil andTitleColor:nil];
    
    self.face_Img.hidden = 1;
    
    self.mob_Tf.delegate = self;
    self.valid_Tf.delegate = self;
    self.botom_ScroView.contentSize = CGSizeMake(screen_width, screen_height*1.2);
    self.botom_ScroView.delegate = self;
    self.botom_ScroView.showsVerticalScrollIndicator = 0;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKb)];
    [self.view addGestureRecognizer:tap];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)showPass:(UIButton *)sender {
    self.pass_Tf.secureTextEntry = 0;
}
//添加人脸
- (IBAction)addFace:(UIButton *)sender {
    [self.view endEditing:1];
    DetectionViewController* dvc = [[DetectionViewController alloc] init];
    dvc.completion=^(NSDictionary *images, UIImage *originImage){
        NSData* data = [[NSData alloc] initWithBase64EncodedString:[images[@"bestImage"] lastObject] options:NSDataBase64DecodingIgnoreUnknownCharacters];
        
        NSString *base_Str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        self.base_Str = base_Str;
      //  [self uploadFace:base_Str];
        NSLog(@"base-------%@",base_Str);
         UIImage* bestImage = [UIImage imageWithData:data];
    
        [self.face_IMge setImage:bestImage forState:UIControlStateNormal];
        self.mutable_title.hidden = 1;
        //self.face_Img.image = bestImage;
    };
    [self pushSecondVC:dvc];
}
- (IBAction)checkRule:(UIButton *)sender {
    [self.view endEditing:1];
   sender.selected = !sender.selected;
    self.checkRule = !self.checkRule;
}
- (IBAction)finish:(Regular_Btn *)sender {
    [self.view endEditing:1];
    if([self.mob_Tf.text isEqualToString:@""]||[self.valid_Tf.text isEqualToString:@""]||[self.pass_Tf.text isEqualToString:@""]){
        [WToast showWithText:@"请填写完整的注册信息"];
        return;
    }
    if(self.checkRule==0){
        [WToast showWithText:@"请勾选服务协议"];
        return;
    }
    NSString *pass = [NSString stringWithFormat:@"%@",self.pass_Tf.text];
    NSDictionary *param = @{@"ctrl":@"live_anchor",
                            @"ac":@"auth",
                            @"op":@"login",
                            @"ta":@"reg",
                            @"mobile":self.mob_Tf.text,
                            @"password":pass,
                            @"captcha":self.valid_Tf.text,
                            @"nickname":self.nick_Name.text,
                            @"face_base64":self.base_Str};
    [self weqPostHandle:param success:^(id response) {
        if(response){
            [WToast showWithText:@"注册成功，请登录"];
            [self.navigationController popViewControllerAnimated:1];
      }
    } failure:^(NSError *error) {
        
    }];
}
-(void)next_Action:(UIButton *)sender{
    [self.view endEditing:1];
    if([self.mob_Tf.text isEqualToString:@""]||[self.valid_Tf.text isEqualToString:@""]||[self.pass_Tf.text isEqualToString:@""]){
        [WToast showWithText:@"请填写完整的注册信息"];
        return;
    }
    NSString *pass = [NSString stringWithFormat:@"%@",self.pass_Tf.text];
    NSDictionary *param = @{@"ctrl":@"live_anchor",@"ac":@"auth",@"op":@"login",@"ta":@"reg",@"mobile":self.mob_Tf.text,
                            @"password":pass,@"captcha":self.valid_Tf.text,@"inviter":self.invation_Tf.text,@" nickname":self.nick_Name.text,@"face_base64":self.base_Str};
    [self weqPostHandle:param success:^(id response) {
        if(response){
            [WToast showWithText:@"注册成功，请登录"];
            [self.navigationController popViewControllerAnimated:1];
    }
    } failure:^(NSError *error) {
        
    }];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.mob_Tf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.mob_Tf.text.length >= 11) {
            self.mob_Tf.text = [textField.text substringToIndex:11];
            return NO;
        }
    }
    return YES;
}
-(void)openCountdown{
    
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [self.authCodeBtn setTitle:@"重新发送" forState:UIControlStateNormal];
               
                self.authCodeBtn.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮显示读秒效果
                [self.authCodeBtn setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", seconds] forState:UIControlStateNormal];
               
                self.authCodeBtn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}
-(void)hideKb{
    [self.view endEditing:1];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:1];
}
-(void)getValid:(UIButton *)sender{
    [self.view endEditing:1];
    if([self.mob_Tf.text isEqualToString:@""]){
        [WToast showWithText:@"请输入正确的手机号"];
        return;
    }
    NSString *md5Str = [NSString stringWithFormat:@"km%@bz",self.mob_Tf.text];
    NSString *tokens = [XTool md5:md5Str];
    NSDictionary *param = @{@"ctrl":@"common",@"mobile":self.mob_Tf.text,@"captcha":tokens,@"ac":@"code"};
    [self weqPostHandle:param success:^(id response) {
        NSLog(@"请求结果===%@",response);
        if(response){
            [WToast showWithText:@"发送成功"];
            [self openCountdown];
        }
    } failure:^(NSError *error) {
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
